﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Collections;

namespace Notifying
{
    public static class HTMLBuilder
    {

        //TODO: Make 2 methods. One for building query results 
        public static string BuildResultHtml_SingleQueryInDocument(User User, string Query, List<Document> Results, HebrewStemmer hs)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(String.Format("<cfmail type=\"HTML\"><div align =center > Dear {0} , Relevant links regarding query : {1} <br><br>", 
                User.FirstName, Query));

            foreach (Document doc in Results)
            {
                sb.Append(FormatResultHTML(doc, ArrangeExcerpts(GetHTMLExcerpts(doc, new string[] { Query } ,hs))));
            }
            sb.Append(String.Format("Update Issued At : {0}</div> </cfmail>", DateTime.Now.ToString()));

            return sb.ToString();
        }


        public static string BuildResultHTML_AllQueriesInDocument(User User, List<Document> Results)
        {
            if (Results.Count == 0)
                return string.Empty; //No results, no Email.

            StringBuilder sb = new StringBuilder();

            sb.Append(String.Format("<cfmail type=\"HTML\"><div align =center dir = rtl lang=he> Dear {0} , Relevant links :  <br><br>",
                User.FirstName));

            foreach (Document doc in Results)
            {
                sb.Append(FormatResultHTML(doc, ArrangeExcerpts(doc.Excerpts)));     
            }
            sb.Append(String.Format("Update Issued At : {0}</div> </cfmail>", DateTime.Now.ToString()));
            return sb.ToString();

        }
        
        

        public static string[] GetHTMLExcerpts(Document Document, string[] Queries, HebrewStemmer hs)
        {
            return Document.GetHTMLExcerpts(Queries,
                   Document.Section.Text, 1, 7, 7, hs);
        }


        public static string FormatResultHTML(Document doc, string Excerpts)
        {
            return (String.Format("{2} - <b>{4}</b> <br>  {0} . {5} <br> {1} <br> {3} <br> <hr> <br>",
                   doc.Fields[(int)Common.Document.Section.Title].Text,
                   makeHREF(doc.url, doc.DisplayNameProperty), doc.DateTime.ToString(),
                    Excerpts, doc.DocumentTypeToString(), (doc.talkbackNumber == 0) ? "" : doc.talkbackNumber.ToString()));

        }

        public static string makeHREF(Uri url, string title)
        {
            return "<A HREF=\"" + url.ToString() + "\"> " + title + "</A>";
        }

        public static string ArrangeExcerpts(string[] Excerpts)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in Excerpts)
            {
                sb.Append(String.Format("{0}<br>", s));
            }
            return sb.ToString();
        }
         
    }
}
